<?
include "../config/constants.php";

try{


	$wsclient = new SoapClient(WS_PORTAL);
	$res = $wsclient->getCIDByStatus(array('_status'=>STATUS_CONTENTS_MMINIT))->return->PortalIntraWebService_struct_cidstatus;

	mysql_connect("localhost", "canss", "1022" ) or die("Could not connect: ".mysql_error());

	if( gettype( $res ) == "object" ) $res = array($res);

	if( $res ) foreach( $res as $i ){

		$i = $i->cid;
		if( $wsclient->getContentStatusByID( array('_cid'=>$i))->return != STATUS_CONTENTS_MMINIT ) continue;

		$SourceMedia = glob(MEDIA_SOURCE_PATH."/khj/$i*");
		$SourceMedia = $SourceMedia[0];

		if( $SourceMedia == null || !file_exists( $SourceMedia ) ){
			//$wsclient->updateContentStatus( array( '_cid'=>$i, '_status'=>STATUS_CONTENTS_CNF) );
/*			$reqc = new HttpRequest(SRV_CONTENT_STATUS_UPDATE, HttpRequest::METH_POST);
			$reqc->addPostFields(array('_cid'=>$i));
			$reqc->addPostFields(array('_status'=>STATUS_CONTENTS_CNF));
			$reqc->send();
*/			$_cid=$i;
			$_status=STATUS_CONTENTS_CNF;
			include "../ServiceManager/UpdateContentStatus.php";

			continue;
		}

		$TargetMedia 		= MEDIA_CONTENT_PATH.'/'.$i;
		$TargetThumbnail	= MEDIA_THUMB_PATH.'/'.$i.'.jpg';

		//$result = $wsclient->updateContentStatus( array('_cid'=>$i, '_status'=>STATUS_CONTENTS_ENCODING));

/*        $reqc = new HttpRequest(SRV_CONTENT_STATUS_UPDATE, HttpRequest::METH_POST);
        $reqc->addPostFields(array('_cid'=>$i));
	    $reqc->addPostFields(array('_status'=>STATUS_CONTENTS_ENCODING));
        $reqc->send();
*/		$_cid=$i;
		$_status=STATUS_CONTENTS_ENCODING;
		include "../ServiceManager/UpdateContentStatus.php";
		
		$req = new HttpRequest(ENC_RULE_LOADER_URL, HttpRequest::METH_POST);
		$req->addPostFields(array('CID' => $i ));
		$EncRule = $req->send()->getBody();
//for ffmpeg5 or higher
if(FFMPEG5ORHIGHER)
{
		$EncRule = str_replace("-author ", "-metadata auther=", $EncRule);
		$EncRule = str_replace("-title ", "-metadata title=", $EncRule);
		$EncRule = str_replace("-copyright ", "-metadata copyright=", $EncRule);
}
//
		$EncRule = str_replace("-acodec \"wmav2\"", "-ac \"2\" -acodec \"wmav2\"", $EncRule);

		$EncRule2 = str_replace("-acodec \"wmav2\"", "-acodec \"mp3\" -ar \"44100\"", $EncRule);
//for ffmpeg5 or higher
if(FFMPEG5ORHIGHER)
{
		$EncRule2 = str_replace("-acodec \"mp3\"", "-acodec \"libmp3lame\"", $EncRule2);
}
//
		$EncRule2 = str_replace("-vcodec \"wmv2\"", "-vcodec \"flv\"", $EncRule2);
		$EncRule2 = str_replace(".asf", ".flv", $EncRule2);
		$EncRule3 = $EncRule;

		$EncRule = $EncRule2; include "Encoder.php";
		$EncRule = $EncRule3; include "Encoder.php";
		include "Thumbnailer.php";
		$_cid=$i; include "EncCallback.php";

		unlink( $SourceMedia );
	}

}catch(exception $e){
	try
	{
		if($fp && $fp!=0)
		pclose($fp);
	}catch(exception $ex)
	{}
	echo "FATAL ERROR<br/>$e";
	echo $wsclient->__getLastRequest();
	exit;	
}

?>
